import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

//TileDisplay is a wrapper for Tile that carries and executes all of the graphical and GUI functions.
public class TileDisplay implements Clickable {
	private Tile tile;
	private int x;
	private int y;
	private BufferedImage image;
	private String sType;
	
	public TileDisplay(Tile t){
		this.tile = t;
		this.x = findX();
		this.y = findY();
		int type = tile.getType();
		sType = "";
		if(type == 1){
			sType = "Brick";
			try {
				image = ImageIO.read(new File("quarry.gif"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(type == 2){
			sType = "Grain";
			try {
				image = ImageIO.read(new File("plains.gif"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(type == 3){
			sType = "Lumber";
			try {
				image = ImageIO.read(new File("forest.gif"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(type == 4){
			sType = "Ore";
			try {
				image = ImageIO.read(new File("mountains.gif"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(type == 5){
			sType = "Wool";
			try {
				image = ImageIO.read(new File("field.gif"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(type == 6){
			sType = "Desert";
			try {
				image = ImageIO.read(new File("desert.gif"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (type == 7){
			sType = "Water";
		}
	}
	
	public void drawSelf(int size, Graphics g){
		int type = tile.getType();
		String sType = "";
		if(type == 1){
			sType = "Brick";
			g.setColor(Color.RED);
		} else if(type == 2){
			sType = "Grain";
			g.setColor(Color.YELLOW);
		} else if(type == 3){
			sType = "Lumber";
			g.setColor(Color.GREEN);
		} else if(type == 4){
			sType = "Ore";
			g.setColor(Color.DARK_GRAY);
		} else if(type == 5){
			sType = "Wool";
			g.setColor(Color.GRAY);
		} else if(type == 6){
			sType = "Desert";
			g.setColor(Color.ORANGE);
		} else if (type == 7){
			sType = "Water";
			g.setColor(Color.CYAN);
		}
			
		int[] xPoints = {size * (x + 1) + x, size * (x + 2) + x, size * (x + 2) + x, 
							size * (x + 1) + x, size * x + x, size * x + x, size * (x + 1) + x};
		int[] yPoints = {size * y + 26 * y, size * y + size / 2 + 26 * y, 
					size * (y + 1) + size / 2 + 26 * y, size * (y + 2) + 26 * y, 
					size * (y + 1) + size / 2 + 26 * y, 
					size * y + size / 2 + 26 * y, size * y + 26 * y};
		g.fillPolygon(xPoints, yPoints, 7);
		g.setColor(Color.BLACK);
		
		if(image != null){
			g.drawImage(image, size * x + x, size * y + 26 * y, null);
		}
		g.setFont(new Font("Helvetica", Font.BOLD, 18));
//		g.setColor(Color.BLACK);
//		g.drawString(sType, size * x + size / 2, size * (y + 1) + size / 2 * y);
		g.drawString(tile.getDieNum() + "", size * x + size / 2 + 5, size * (y + 1) + size / 2 * y + 15);
//		g.drawString(tile.getSlot() + "", size * x + size / 2 + 5, size * (y + 1) + size / 2 * y + 30);
		g.setFont(new Font("Helvetica", Font.PLAIN, 14));
		g.setColor(Color.BLACK);
	}
	
	private int findX() {
		// TODO Auto-generated method stub
		int slot = tile.getSlot();
		if (slot == 35)
			return 1;
		else if (slot == 34 || slot == 36)
			return 2;
		else if	(slot == 8 || slot == 33 || slot == 37)
			return 3;
		else if(slot == 4 || slot == 13 || slot == 20 || slot == 32)
			return 4;
		else if(slot == 1 || slot == 9 || slot == 17)
			return 5;
		else if(slot == 5 || slot == 14 || slot == 21 || slot == 31)
			return 6;
		else if(slot == 2 || slot == 10 || slot == 18)
			return 7;
		else if(slot == 6 || slot == 15 || slot == 22 || slot == 30)
			return 8;
			else if(slot == 3 || slot == 11 || slot == 19)
			return 9;
		else if(slot == 7 || slot == 16 || slot == 23 || slot == 29)
			return 10;
		 else if (slot == 12 || slot == 24 || slot == 28 )
			return 11;
		 else if (slot == 25 || slot == 27)
			 return 12;
		 else if (slot == 26)
			 return 13;
		 else
			 return 14;
	}

	private int findY() {
		// TODO Auto-generated method stub
		int slot = tile.getSlot();
		if (slot > 19 && slot <= 23)
			return 0;
		else if (slot < 4 || slot == 37 || slot == 24)
			return 1;
		else if(slot < 8 || slot == 25 || slot == 36)
			return 2;
		else if(slot < 13 || slot == 35 || slot == 26)
			return 3;
		else if(slot < 17 || slot == 34 || slot == 27)
			return 4;
		else if (slot < 20 || slot == 28 || slot == 33)
			return 5;
		else
			return 6;
	}

	public Tile getTile() {
		return tile;
	}
	public void setTile(Tile tile) {
		this.tile = tile;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}

	@Override
	//checks if the x y coordinates of the mouse click are within the tile
	public boolean overlaps(int px, int py) {
		int size = SOCApplet.SIZE;
		if(px > (size * x + x + size / 2) && px < (size * (x + 1) + x + size / 2) &&
			py > (size * y + size / 4 + 26 * y) && py < (size * (y + 2) + 26 * y - size / 4)){
			System.out.println("Clicked tile # " + tile.getSlot());
			return true;
		}
		return false;
	}
	
}
